import {
	ApiHost,
	TokenKey
} from '@/config/index.js';
import {
	mapState,
	mapGetters
} from 'vuex';
import dayjs from 'dayjs'
export default {
	computed: {
		...mapGetters('user', ['isLogin']),
		...mapState('user', {
			userInfo(state) {
				return {
					...state.userInfo,
					hide_mobile: state.userInfo.mobile ? state.userInfo.mobile.replace(/^(\d{3})\d{4}(\d{4})$/,
						"$1****$2") : ''
				}
			}
		}),
		...mapState('config', {
			appConfig(state) {
				return state.info;
			}
		}),
		...mapState('lang',['converter','lang'])
	},
	filters: {
		formatDate(v) {
			return dayjs(v * 1000).format('YYYY-MM-DD HH:mm:ss')
		}
	},
	onShow() {
		// 隐藏原生tabbar
		// uni.hideTabBar({
		// 	success() {
		// 		console.log('隐藏');
		// 	}
		// })
		this.$nextTick(() => {
			uni.$emit('pageShow', this.getCurrentPage())
		})
	},
	onHide() {
		this.$nextTick(() => {
			uni.$emit('pageHide', this.getCurrentPage())
		})
	},
	onPageScroll(e) {
		uni.$emit('onPageScroll', e)
	},
	methods: {
		//更新登录状态
		updateAuth() {
			let token = uni.getStorageSync(TokenKey);
			// this.isLogin = !!token;
		},
		getCurrentPage() {
			let pages = getCurrentPages();
			if (pages.length) {
				return pages[pages.length - 1].$page.fullPath
			}
			return null;
		},
		toWebview(url,obj) {
			if (!url) return;
			if (String(url).startsWith('/')) {
				uni.$u.route('/pages/webview/webview?url=' + ApiHost + url + '&name=' + obj)
			} else {
				uni.$u.route('/pages/webview/webview?url=' + url + '&name=' + obj)
			}
		},
		backPage() {
			// #ifdef APP
			let routes = getCurrentPages();
			if (routes.length == 1) {
				uni.switchTab({
					url: '/pages/index/index'
				})
			} else {
				uni.navigateBack()
			}
			// #endif

			//#ifdef H5
			let isApp = window.localStorage.getItem('app')
			if (isApp) {
				try {
					let isIos = window.localStorage.getItem('ios')
					let action = JSON.stringify({
						action: "goBack",
					})
					if (isIos) {
						window.webkit.messageHandlers.callbackHandler.postMessage(action);
					} else {
						Android.webCallback(action)
					}
				} catch (e) {
					console.log("跳转失败了",e)
					alert("跳转失败了")
				}
			} else {
				let routes = getCurrentPages();
				if (routes.length == 1) {
					uni.switchTab({
						url: '/pages/index/index'
					})
				} else {
					uni.navigateBack()
				}
			}
			// #endif
		},
		toLocalWebview(url) {
			uni.setStorage({
				key: 'url',
				data: url,
				success() {
					uni.navigateTo({
						url: '/pages/webview/webview-storage'
					})
				}
			})
		},
		handleLang(str){
			return this.converter(str)
		}
	}
}